﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ServiceFramework
{
    public class HeartbeatServiceComponent : ServiceComponentBase
    {
        private readonly int _heartbeatFrequency;

        /// <summary>
        /// Creates a new instance of the HeartbeatComponent class.
        /// </summary>
        /// <param name="heartbeatFrequency">The heartbeat frequency (beats per second).</param>
        public HeartbeatServiceComponent(int heartbeatFrequency)
            : base("HeartbeatComponent", 1)
        {
            if (heartbeatFrequency < 1)
            {
                throw new ArgumentException("Must be 1 or greater.", "heartbeatFrequency");
            }

            _heartbeatFrequency = heartbeatFrequency;
        }

        //public void SendMessage(Message message)
        //{
        //    throw new System.NotImplementedException();
        //}

        //public override void Execute()
        //{
        //    bool isRunning = true;
        //    int delayMilliseconds = 1/_heartbeatsPerSecond;

        //    while (isRunning)
        //    {
        //        SendMessage(Message.CreateHeartbeatMessage());
        //        Thread.Sleep(delayMilliseconds);
        //    }
        //}

        public override void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("heartbeatFrequency");
            writer.WriteAttributeString("beatsPerSecond", _heartbeatFrequency.ToString());
            writer.WriteEndElement();
        }
    }
}
